<?php


namespace app\seller\controller;

use think\Db;
use addons\ttlock\ttlock;
use think\Config;
use app\common\service\Wechat;


class Fangjian extends Base
{
    public function index()
    {
        $length = input('length');
        $list = Db::name('fangjian')->where('category_id', $this->shop_id)->limit($length, 30)->select();
        foreach ($list as &$item) {
            $item['image'] = cdnurl($item['image'], true);

        }
        return $this->success('获取成功', $list);
    }

    public function add()
    {
        $data = input();
        if (!$data['name']) {
            return $this->error("请输入包房名称");
        }
        if (!$data['type']) {
            return $this->error("请输入类型");
        }
        if (!$data['price']) {
            return $this->error("请输入价格");
        }
        if (!$data['booking_time']) {
            return $this->error("请输入起订小时");
        }
        if (!$data['image']) {
            return $this->error("请上传图片");
        }
        $res = Db::name('fangjian')->insertGetId([
            'type' => $data['type'],
            'price' => $data['price'],
            'createtime' => time(),
            'category_id' => $this->shop_id,
            'name' => $data['name'],
            'image' => $data['image'],
            'package_vip_price' => $data['package_vip_price'],
            'biaoqian' => $data['biaoqian'],
            'images' => $data['images'],
            'room_lock_id' => $data['room_lock_id'],
            'dian_id' => $data['dian_id'],
            'booking_time' => $data['booking_time'],
            'duration' => $data['duration'],
            'message_devid' => $data['message_devid'],
        ]);
        if (isset($data['package']) && $data['package']) {
            $packages = [];
            foreach ($data['package'] as $package) {
                $packages[] = [
                    'fj_id' => $res,
                    'price' => $package['price'],
                    'times' => $package['times'],
                    'uniacid' => config('site.uniacid')
                ];
            }
            Db::name('fangjian_package')->insertAll($packages);
        }
        if (!$res) {
            return $this->error("操作失败");
        }
        return $this->success('操作成功');
    }

    public function doedit()
    {
        $data = input();
        if (!$data['name']) {
            return $this->error("请输入包房名称");
        }
        if (!$data['type']) {
            return $this->error("请输入类型");
        }
        if (!$data['price']) {
            return $this->error("请输入价格");
        }
        if (!$data['booking_time']) {
            return $this->error("请输入起订小时");
        }
        if (!$data['image']) {
            return $this->error("请上传图片");
        }
        $res = Db::name('fangjian')->where([
            'category_id' => $this->shop_id,
            'id' => $data['id']
        ])->update([
            'type' => $data['type'],
            'price' => $data['price'],
            'name' => $data['name'],
            'image' => $data['image'],
            'biaoqian' => $data['biaoqian'],
            'images' => $data['images'],
            'room_lock_id' => $data['room_lock_id'],
            'package_vip_price' => $data['package_vip_price'],
            'dian_id' => $data['dian_id'],
            'booking_time' => $data['booking_time'],
            'duration' => $data['duration'],
            'message_devid' => $data['message_devid'],
        ]);
        Db::name('fangjian_package')->where(['fj_id' => $data['id']])->delete();

        if (isset($data['package']) && $data['package']) {
            $packages = [];
            foreach ($data['package'] as $package) {
                $packages[] = [
                    'fj_id' => $data['id'],
                    'price' => $package['price'],
                    'times' => $package['times'],
                    'uniacid' => config('site.uniacid')
                ];
            }
            Db::name('fangjian_package')->insertAll($packages);
        }
        if ($res === false) {
            return $this->error("操作失败");
        }
        return $this->success('操作成功');
    }

    public function del()
    {
        $id = input('id');
        $res = Db::name('fangjian')->where([
            'id' => $id,
            'category_id' => $this->shop_id,
        ])->delete();
        if ($res !== false) {
            return $this->success('操作成功');
        }
        return $this->error('操作失败');
    }

    public function edit()
    {
        $id = input('id');
        $detail = Db::name('fangjian')->where('category_id', $this->shop_id)->where('id', $id)->find();
        if ($detail) {
            $detail['image'] = cdnurl($detail['image'], true);
            $images = explode(",", $detail['images']);
            foreach ($images as &$item) {
                $item = cdnurl($item, true);
            }
            $detail['images'] = $images;

            $detail['package'] = Db::name('fangjian_package')->where(['fj_id' => $id])->select();

            return $this->success('操作成功', $detail);
        }
        return $this->error("操作失败");
    }

    public function closing()
    {
        $ids = input("id");
        $data = Db::name('fangjian')->alias('b')
            ->field("b.id,b.dian_id")
            ->where("id", $ids)
            ->find();
        if (!$data) {
            return $this->error("房间不存在");
        }
        if (!$data['dian_id']) {
            return $this->error("房间关电命令不存在");
        }
        $res = Db::name("diancommand")->insert([
            'fangjian_id' => $data['id'],
            'status' => 2,
            'is_status' => 0,
            'create_time' => time(),
        ]);
        if (!$res) {
            return $this->error("写入失败");
        }
        return $this->success('等待服务器发送,耐心等待几秒钟');
    }

    public function opening()
    {
        $ids = input("id");
        $data = Db::name('fangjian')->alias('b')
            ->field("b.id,b.dian_id")
            ->where("id", $ids)
            ->find();
        if (!$data) {
            return $this->error("房间不存在");
        }
        if (!$data['dian_id']) {
            return $this->error("房间开电命令不存在");
        }
        $res = Db::name("diancommand")->insert([
            'fangjian_id' => $data['id'],
            'status' => 1,
            'is_status' => 0,
            'create_time' => time(),
        ]);
        if (!$res) {
            return $this->error("写入失败");
        }
        return $this->success('等待服务器发送,耐心等待几秒钟');
    }

    /**
     * 获取开锁密码
     */
    public function password()
    {
        $fj_id = input('fj_id');
        $detail = Db::name('fangjian')
            ->field("room_lock_id,category_id")
            ->where('id', $fj_id)
            ->find();
        if (!$detail) {
            $this->error('房间不存在');
        }

        $ttlock = new ttlock();
        $category_hall_lock_id = Db::name('category')->where('id', $detail['category_id'])->value('hall_lock_id');
        if ($category_hall_lock_id) {
            $hall_lock = $ttlock->getPassword($category_hall_lock_id, "hall_" . $fj_id);
            if ($hall_lock) {
                $hall_lock_id = $hall_lock;
            } else {
                $hall_lock_id = '密码获取失败';
            }
        } else {
            $hall_lock_id = '-';
        }

        if ($detail['room_lock_id']) {
            $room_lock = $ttlock->getPassword($detail['room_lock_id'], "room_" . $fj_id);
            if ($room_lock) {
                $room_lock_id = $room_lock;
            } else {
                $room_lock_id = '密码获取失败';
            }
        } else {
            $room_lock_id = '-';
        }
        return $this->success('查询成功', [
            'hall_lock_id' => $hall_lock_id,
            'room_lock_id' => $room_lock_id
        ]);

    }

    public function getCode()
    {
        $id = input('id');
        $oWechat = new Wechat();
        $path = $oWechat->qrcode($id, 'pages/goodsdetaile/goodsdetaileNew');
        return $this->success('查询成功', [
            'path' => cdnurl($path, true),
        ]);
    }

    public function dasao()
    {
        $id = input('id');
        $state = input('state');
        Db::name('fangjian')
            ->where('id', $id)
            ->update([
                'is_dasao' => $state
            ]);
        $this->success('操作成功');
    }
}
